using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.GamerServices;
using Trippy.Animation;
using Trippy.Abilities;
using Trippy.CollidableGameComponents;
using Trippy.CollidableGameComponents.Objects;
using Trippy.Levels;


// these enemies are pathing enemies
namespace Trippy.CollidableGameComponents.Agents
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    ///
    public enum PathEnemyState { None, Grounded }

    public class PathingEnemy : Agent
    {
        public static Texture2D SpriteSheet { get; private set; }

        private Vector2 actPositionRight;
        private Vector2 actPositionLeft;

        protected override void LoadContent()
        {
            if (SpriteSheet == null) SpriteSheet = CurrentLevel.Game.Content.Load<Texture2D>("Sprites/Agents/dragon_sheet2");
            Sprite = new AnimatedSprite(this, SpriteSheet, 90, 100, 0, 0, 0, 0, 4, 0.07f);
            Sprite.ChangeAnimation(1);

            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            StandardDraw(gameTime);
            base.Draw(gameTime);
        }

        public PathEnemyState State { get; protected set; }

        public PathingEnemy(Level level, Vector2 initialPosition)
            : base(level)
        {
            actPositionLeft = initialPosition + new Vector2(-300, 0);
            actPositionRight = initialPosition + new Vector2(300, 0);

            // important! flying enemies dont have friction, which decreases their velocity (very fast!)
            Friction = 0;

            State = PathEnemyState.None;

        }
        public override void HitGround(float y, Vector2 vel = new Vector2())
        {
            State = PathEnemyState.Grounded;
            base.HitGround(y);
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>

        public override void Update(GameTime gameTime)
        {
            float elapsedTime = TrippyUtilities.CalculateElapsedTime(gameTime);

            if (Position.X > actPositionRight.X)
            {
                
                Velocity = new Vector2(-100, Velocity.Y);
                Position = new Vector2(actPositionRight.X, Position.Y);

                
                Sprite.ChangeAnimation(0);
            }


            if (Position.X < actPositionLeft.X)
            {
                Velocity = new Vector2(100, Velocity.Y);
                Position = new Vector2(actPositionLeft.X, Position.Y);


                Sprite.ChangeAnimation(1);

            }

            Sprite.Step();
            UpdateStandardPhysics(elapsedTime);

            base.Update(gameTime);
        }

        protected override void CollisionHandler(CollidableGameComponent component, CollidableGameComponent.CollisionSide side)
        {
            if (component is Hero)
            {
                Hero h = (Hero)component;
                switch (side)
                {
                    case CollisionSide.Top:
                        h.Bounce(BoundingBox.Top);
                        break;
                    case CollisionSide.Right:
                        h.Die(Hero.MannerOfDeath.Enemy);
                        break;
                    case CollisionSide.Bottom:
                        h.Die(Hero.MannerOfDeath.Enemy);
                        break;
                    case CollisionSide.Left:
                        h.Die(Hero.MannerOfDeath.Enemy);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("side");
                }
            }

            base.CollisionHandler(component, side);
        }


        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }


        public override void Restore()
        {
            Sprite.Tint = Color.White;
            base.Restore();
            Velocity = new Vector2(100f, 0f);
        }
    }
}